package com.jie.langlangmall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.mapper.SmsHomeNewProductMapper;
import com.jie.langlangmall.model.SmsHomeNewProduct;
import com.jie.langlangmall.model.SmsHomeNewProductExample;
import com.jie.langlangmall.service.SmsHomeNewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/25
 * @Email 3217998214@qq.com
 * @Description 新品推荐
 */
@Service
public class SmsHomeNewProductServiceImpl implements SmsHomeNewProductService {

    @Autowired
    private SmsHomeNewProductMapper homeNewProductMapper;

    /**
     * 添加新品推荐
     *
     * @param homeNewProductList
     */
    @Override
    @Transactional
    public void create(List<SmsHomeNewProduct> homeNewProductList) {
        for (SmsHomeNewProduct SmsHomeNewProduct : homeNewProductList) {
            SmsHomeNewProduct.setRecommendStatus(1);
            SmsHomeNewProduct.setSort(0);
            homeNewProductMapper.insert(SmsHomeNewProduct);
        }
    }

    /**
     * 更新排序
     *
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {

    }

    /**
     * 删除新品
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        example.createCriteria().andIdIn(ids);
        homeNewProductMapper.deleteByExample(example);
    }

    /**
     * 更新状态
     *
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeNewProduct homeNewProduct = new SmsHomeNewProduct();
        homeNewProduct.setRecommendStatus(recommendStatus);
        homeNewProductMapper.updateByExampleSelective(homeNewProduct, example);
    }

    /**
     * 分页查询
     *
     * @param productName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<SmsHomeNewProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        SmsHomeNewProductExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(productName)) {
            criteria.andProductNameLike("%" + productName + "%");
        }
        if (recommendStatus != null) {
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        return homeNewProductMapper.selectByExample(example);
    }
}
